package com.paireach.bw.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.utils.NetworkUtils;
import com.paireach.bw.R;
import com.paireach.bw.adapter.CommonLoadAdapter;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.LocationInfo;
import com.paireach.bw.bean.OrderResult;
import com.paireach.bw.bean.OrdersBean;
import com.paireach.bw.bean.ReturnInfo;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.fragment.CommonFrag212;
import com.paireach.bw.fragment.CommonFrag220;
import com.paireach.bw.fragment.CommonFrag226;
import com.paireach.bw.fragment.CommonFrag228;
import com.paireach.bw.fragment.CommonFrag230;
import com.paireach.bw.fragment.CommonFrag238;
import com.paireach.bw.fragment.CommonFrag240;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.GsonUtil;
import com.paireach.bw.utils.LogUtil;
import com.paireach.bw.view.CustomDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${cqc} on 2017/8/31.
 * 没作用
 */

public class NewDriverOrderActivity extends BaseActivity {

    @BindView(R.id.img_btn_back)
    ImageButton imgBtnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private List<OrdersBean> orderList = new ArrayList<>();
    private String orderCode;

    private FragmentManager fm;
    private CommonLoadAdapter adapter;
    private TextView tv_order_num_parent;
    private TextView tv_origin_address_parent;
    private TextView tv_weight;
    private TextView tv_contacts_parent;
    private TextView tv_contacts_tel_parent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_driver_order);
        orderCode = getIntent().getStringExtra("orderCode");
        ButterKnife.bind(this);

        fm = getSupportFragmentManager();
        showProgressDialog();
        new WayBillRequest().getOrderCodeDetail(phoneNumber, orderCode);
        initTitleBar();
    }

    private void initTitleBar() {
        tvTitle.setText("消息通知");
        btnCommit.setVisibility(View.GONE);
        imgBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.addItemDecoration(new CustomDividerItemDecoration(context, CustomDividerItemDecoration.VERTICAL));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        adapter = new CommonLoadAdapter(orderList);
        recyclerView.setAdapter(adapter);

        View headView = LayoutInflater.from(context).inflate(R.layout.header_common_212, recyclerView, false);
        adapter.addHeaderView(headView);
        findHeadViews(headView);

        View footView = LayoutInflater.from(context).inflate(R.layout.footer_ka_common, recyclerView, false);
        adapter.addFooterView(footView);
        initFootView(footView);
    }


    private void initFootView(View footView) {
        TextView tv_footer = (TextView) footView.findViewById(R.id.tv_footer);
        tv_footer.setText("确认接单");
        tv_footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetworkUtils.isConnected()) {
                    showProgressDialog();

                    LocationInfo location = ((MyApplication) getApplication()).locationInfo;
                    new WayBillRequest().driverAcceptOrder(phoneNumber, orderCode, "", location.getLongitude() + "", "" + location.getLatitude(), driverNum);
                } else {
                    showToast("网络连接不可用，请稍后重试");
                }
            }
        });
    }

    private void findHeadViews(View headView) {
        tv_order_num_parent = (TextView) headView.findViewById(R.id.tv_order_num_parent);
        tv_origin_address_parent = (TextView) headView.findViewById(R.id.tv_origin_address_parent);
        tv_weight = (TextView) headView.findViewById(R.id.tv_weight);
        tv_contacts_parent = (TextView) headView.findViewById(R.id.tv_contacts_parent);
        tv_contacts_tel_parent = (TextView) headView.findViewById(R.id.tv_contacts_tel_parent);
    }

    private void initHeadViews(OrdersBean info) {
        tv_order_num_parent.setText("负载单号：" + orderCode);
        tv_origin_address_parent.setText("发货地址：" + info.getFRM_SHPG_ADDR());
        tv_weight.setText("货物重量：" + info.getBW_WGT() + "kg");
        tv_contacts_parent.setText("联系人：" + info.getDRIVER_NAME());
        tv_contacts_tel_parent.setText("电话：" + info.getDRIVER_MOBILE());
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        showToast(event.getErrorMessage());
        if (event.getRequestTag() == RequestTag.GET_ORDER_CODE_DETAIL || event.getRequestTag() == RequestTag.DRIVER_ACCEPT_ORDER) {
            finish();
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.GET_ORDER_CODE_DETAIL) {
            progressBar.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(event.getJson())) {
                showToast("没有获取到运单详情");
            } else {
                OrderResult orderResult = GsonUtil.jsonToBean(event.getJson(), OrderResult.class);
                if (orderResult.getStatus() == 1) {
                    OrdersBean info = orderResult.getOrders().get(0);
                    initHeadViews(info);
                    orderList.addAll(orderResult.getOrders());
                    adapter.notifyDataSetChanged();
                }
            }
        } else if (event.getRequestTag() == RequestTag.DRIVER_ACCEPT_ORDER) {
            ReturnInfo info = GsonUtil.jsonToBean(event.getJson(), ReturnInfo.class);
            showToast(info.getMsg());
            if (info.getStatus() == 1) {
                BDTraceUtil.openTrace();
                finish();
            }
        }
    }
}

